package com.example.framework;

import javax.swing.JDialog;

import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JLabelOperator;
import org.netbeans.jemmy.operators.JTextFieldOperator;

public class HelpersBase {
	
	protected final ApplicationManager manager;
	protected final JFrameOperator mainFrame;

	public HelpersBase(ApplicationManager applicationManager) {
		this.manager = applicationManager;
		this.mainFrame = manager.getApplication();
	}

	protected String waitMessageDialog(String title, int timeout) {
		long startTime = System.currentTimeMillis();
		long currentTime = startTime;
		while (currentTime < startTime + timeout) {
			JDialog dialog = JDialogOperator.findJDialog(mainFrame.getOwner(), title, false, false);
			if (dialog != null) {
				JDialogOperator dialogOp = new JDialogOperator(dialog);
				String message = new JLabelOperator(dialogOp).getText();
				dialogOp.requestClose();
				return message;
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			currentTime = System.currentTimeMillis();
		}
		return null;
	}
	
	public void pushButtonInDialog(String buttonName) {
		JDialogOperator dialog = new JDialogOperator(mainFrame);
		new JButtonOperator(dialog, buttonName).push();
	}
	
	public void enterTextInDialog(String text) {
		JDialogOperator dialog = new JDialogOperator(mainFrame);
		new JTextFieldOperator(dialog).setText(text);
	}

}
